/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package proyecto.so1;

import java.net.*;
import java.io.*;

/**
 *
 * @author antonio
 */
public class GetSenderMSG extends Thread implements Runnable{
    
    private boolean active;
    
    
    public GetSenderMSG(){
        active = true;
    }
    
    @Override
    public void run(){
        while(active){
            try{ 
                String sender = sender();
                if(!sender.equals("")){
                    principal.sender = sender;
                    this.sleep(2000);
                }
            }
            catch(Exception e){ }
        }
    }
    
    
    private String sender(){
        String sender = "";    
            try{
                Socket client = new Socket("192.168.1.8", 9998);
                client.setReuseAddress(true);
                System.out.println("Connected to " + client.getInetAddress() + " on port "
                + client.getPort() + " from port " + client.getLocalPort() + " of "
                + client.getLocalAddress());
                InputStream input = client.getInputStream();
                DataInputStream data = new DataInputStream(input);
                principal.receiveMSG = data.readBoolean();
                sender = "192.168.1.8";
                client.close();
            }
            catch(Exception e){
                try{
                    Socket client = new Socket("192.168.1.20", 9998);
                    client.setReuseAddress(true);
                    System.out.println("Connected to " + client.getInetAddress() + " on port "
                    + client.getPort() + " from port " + client.getLocalPort() + " of "
                    + client.getLocalAddress());
                    InputStream input = client.getInputStream();
                    DataInputStream data = new DataInputStream(input);
                    principal.receiveMSG = data.readBoolean();
                    sender = "192.168.1.20";
                    client.close();
                }
                catch(Exception ex){ }
            }
            return sender;
    }
    
}
